<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jedrzej\Pimpable\PimpableTrait;
use Spatie\Permission\Models\Role as BaseRole;

class Role extends BaseRole
{
    use HasFactory, PimpableTrait,SoftDeletes;

    /**
     * 可批量赋值字段
     */
    public $fillable = [
        'name',
        'cn_name',
        'guard_name',
        'status',
        'roles',
    ];

    /**
     * 可需查询的字段
     */
    public $searchable = [
        '%name%'
    ];
    /**
     * 状态
     */
    public  $Frontstatus = [
        'noshow' => '不显示',
        'yesshow' => '显示',
    ];

    /**
     * 默认排序字段
     */
    protected $defaultSortCriteria = ['id,desc'];

    /**
     * 可排序字段
     */
    public $sortable = [
        'id',
    ];
    /**
     * 角色属于与其守卫关联的模型的某些用户.
     */
    public function permissions(): BelongsToMany
    {
        return $this->belongsToMany(
            Permission::class,//远程模型
            'role_has_permissions',//中间表
            'role_id',//本身id和中间表的id
            'permission_id',//中间表的id与远程的id
        );
    }
}
